$(function () {
    var mySwiper = new Swiper('.swiper-container', {
        loop: true,
        // 如果需要分页器
        pagination: '.swiper-pagination',
        autoplay: 2000,
    });

    addAndSubtract();
    addProductHtml();
    colorType();


    $('#collection').tap(function () {
        var success = $('#success');
        $(this).toggleClass('on');
        var thisClass = $(this).attr('Class');
        if (thisClass == 'on') {
            success.text('收藏成功！！！');
            success.animate({opacity: 1}, 500, 'ease-in-out', function () {
                setTimeout(function () {
                    success.css('opacity', 0);
                }, 2000);

            });
        } else {
            success.text('取消收藏成功！！！')
            success.animate({opacity: 1}, 500, 'ease-in-out', function () {
                setTimeout(function () {
                    success.css('opacity', 0);
                }, 2000);
            });
        }
    });
    $('#myShop').tap(function () {
        var _this = $(this);
        _this.next().animate({opacity: 1}, 500, 'ease-in-out', function () {
            setTimeout(function () {
                _this.next().css('opacity', 0);
            }, 2000);
        });
        var ids = Params().id;
        var number = $('#number').val();
        var postDatas = {
            postType: 'addCart',
            id: ids,
            num: number,
            color: $('#colorType dd.on').text()
        };
        qingQiuPhp('post', 'admin/productList.php', postDatas, function (res) {
            alert(res.msg);
        });
    });
    $('#payNow').click(function () {
        var id = Number($(this).attr('data-id'));
        var number = $('#number').val();
        var postDatas = {
            postType: 'addCart',
            id: id,
            num: number,
            color: $('#colorType dd.on').text()
        };
        qingQiuPhp('post', 'admin/productList.php', postDatas, function (res) {
            if (res.status == 1) {
                LsySession.setItem('checkItems', [res.sid]);
                window.location.href = 'confirm_order.html';
            } else {
                alert(res.msg);
            }
        });

    });

    function addProductHtml() {
        var myUrlId = Params().id;
        if (myUrlId) {
            qingQiuPhp('get', 'admin/pro_details.php', {getType: 'getList', proId: myUrlId}, function (res) {
                createHtml(res);
            });
        } else {
            qingQiuPhp('get', 'admin/pro_details.php', {getType: 'getDefault'}, function (res) {
                createHtml(res);
            });
        }

    }

    function createHtml(res) {
        var colorStr = '<dt>颜 色</dt>';
        var colors = res[0].color.split(/[,\;\uff0c\.]/);
        colors = colors ? colors : [];
        for (var i = 0, n = colors.length; i < n; i++) {
            if (i == 0) {
                colorStr += '<dd class="on">' + colors[i] + '</dd>';
            } else {
                colorStr += '<dd>' + colors[i] + '</dd>';
            }
        }
        $('#colorType').html(colorStr);
        $('#title').text(res[0].title);
        $('#price').text(Number(res[0].price).toFixed(2));
        $('#oldPrice').text('￥' + res[0].oldPrice);
        $('#sales').text(res[0].sales);
        $('#stock').text(res[0].stock);
        $('#evaluate').text(res[0].evaluate);
        $('.swiper-slide img').attr("src", res[0].imgSrc);
        $('#payNow').attr('data-id', res[0].id);
    }

    function addAndSubtract() {
        var number = $('#number');
        $('#plus').tap(function () {
            var num = number.val();
            num++;
            number.val(num);
        });
        $('#reduce').tap(function () {
            var num = number.val();
            if (num > 1) {
                num--;
                number.val(num);
            }
        });
    }

    $('#number')
        .keyup(function () {
            var _vm = $(this);
            var num = _vm.val();
            _vm.val(num.replace(/^0+|[^\d]/g, ''));
        })
        .blur(function () {
            var _vm = $(this);
            var num = _vm.val();
            if (!num) {
                _vm.val(1);
            }
        })

    function colorType() {
        $('#colorType').on('tap', 'dd', function () {
            var _vm = $(this);
            _vm.addClass('on').siblings().removeClass('on');
        });
    }
});















